package org.jeecg.modules.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.config.wlhy.AipOcrClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;

@Api(tags = "AipOcr")
@RestController
@RequestMapping("/ocr")
@Slf4j
public class AipOcrController {

    @Resource
    AipOcrClient aipOcrClient;

    /**
     * 营业执照
     *
     * @param image
     * @return
     */
    @AutoLog(value = "ocr-营业执照")
    @ApiOperation(value = "ocr-营业执照", notes = "ocr-营业执照")

    @PostMapping(value = "/businessLicense")
    public Result<JSONObject> businessLicense(@RequestParam String image) {
        try {
            JSONObject jsonObject = aipOcrClient.businessLicenseUrl(image);
            log.info("ocr-营业执照识别 {}", jsonObject);
            return Result.OK(jsonObject);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 银行卡
     *
     * @param image
     * @return
     */
    @AutoLog(value = "ocr-银行卡")
    @ApiOperation(value = "ocr-银行卡", notes = "ocr-银行卡")
    @PostMapping(value = "/bankcardUrl")
    public Result<JSONObject> bankcardUrl(@RequestParam String image) {
        try {
            JSONObject jsonObject = aipOcrClient.bankcardUrl(image);
            log.info("ocr-银行卡 {}", jsonObject);
            return Result.OK("识别成功",jsonObject);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 道路运输证识别
     *
     * @param image
     * @return
     */
    @AutoLog(value = "ocr-道路运输证识别")
    @ApiOperation(value = "ocr-道路运输证识别", notes = "ocr-道路运输证识别")
    @PostMapping(value = "/roadTransportCertificate")
    public Result<JSONObject> roadTransportCertificate(@RequestParam String image) {
        try {
            JSONObject jsonObject = aipOcrClient.roadTransportCertificateUrl(image);
            log.info("ocr-道路运输证识别 {}", jsonObject);
            return Result.OK("识别成功",jsonObject);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 行驶证
     *
     * @param image
     * @return
     */
    @AutoLog(value = "ocr-行驶证")
    @ApiOperation(value = "ocr-行驶证", notes = "ocr-行驶证")
    @PostMapping(value = "/vehicleLicenseUrl")
    public Result<JSONObject> vehicleLicenseUrl(@RequestParam String image) {
        try {
            JSONObject frontObj = aipOcrClient.vehicleLicenseUrl(image, "front");
            log.info("ocr-行驶证主页 {}", frontObj);
            JSONObject backObj = aipOcrClient.vehicleLicenseUrl(image, "back");
            log.info("ocr-行驶证副页 {}", backObj);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("行驶证主页",frontObj);
            jsonObject.put("行驶证副页",backObj);
            return Result.OK("识别成功",jsonObject);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }


}
